try:
    import pyi_splash
    pyi_splash.close()
except ImportError:
    pass
 
import pyautogui
from PIL import ImageTk,Image
import time,datetime
import tkinter as tk
from tkinter import *
from tkinter import colorchooser
 
#clock类
class Clock(tk.Tk):
    def __init__(self) -> None:
        
        """初始化"""
        
        self.backgroundlabel=None
        self.width=550
        self.fw,self.fh=pyautogui.size() #屏幕大小
        self.height=40
        self.fontsize=20           #字体大小
        self.fullbool=True;
        super().__init__()
        self.days=["Monday","Tuesday","Wednsday","Thursday","Friday","Saturday","Sunday"]
        self.transbool=True
        self.backgroundcolor="black"
        self.x, self.y = 0, 0
        self.clocklen=26
        self.window_size = '265x10'
        self.attributes("-topmost", 0)
        self.configure(bg=self.backgroundcolor)
        self.time_text = ""
        self.overrideredirect(1)
        self.font = "ds-digital"
        self.attributes("-alpha", 0.5)
        self.attributes("-transparentcolor", "#7F7F7F")
        self.attributes("-topmost", True)
        lbl_fg="#00FFFF"
        
        #函数
        
        def addl(event):       #秒数加1
            if self.clocklen<26:
                self.clocklen+=1
        def del1(event):       #秒数减1
            if self.clocklen>19:
                self.clocklen-=1    
                
        #全屏
        
        def activatefull():
            if self.fullbool==True:
                self.window_size=str(self.width)+"x"+str(self.height)
                self.width=self.fw
                self.height=self.fh
                self.fontsize=110
                self.clocklen=19
                self.geometry(f"{self.fw}x{self.fh}+-7+0")
            else:
                self.window_size="265x10"
                self.width=550
                self.height=40
                self.fontsize=20
                self.clocklen=26
            self.fullbool=not self.fullbool
        
        #设置字体
        
        def setfontmc():
            self.font="Monotype Corsiva"
        def setfontarial():
            self.font="Ink Free"
        def setfontdigital():
            self.font="ds-digital"
        def setfontimpact():
            self.font="Impact"
        def setfontsegoe():
            self.font="Impact"
        def setfontcomic():
            self.font="Comic Sans MS"
        def setfontarialn():
            self.font="Arial"
        def setfonttimes():
            self.font="Times New Roman"
            
        #颜色，本来用lambda实现，但不知道为什么不行
        class bc:
            def colorblack():
                self.backgroundcolor="black"
            def colorgrey():
                self.backgroundcolor="#333333"
            def colorwhite():
                self.backgroundcolor="white"
            def colorblue():
                self.backgroundcolor="blue"
            def colorgreen():
                self.backgroundcolor="green"
            def coloryellow():
                self.backgroundcolor="yellow"
            def colorred():
                self.backgroundcolor="red"
            def colorpurple():
                self.backgroundcolor="purple"
            def colororange():
                self.backgroundcolor="orange"
            def colorbrown():
                self.backgroundcolor="brown"
            def colorpink():
                self.backgroundcolor="pink"
            def coloralpha():
                self.backgroundcolor="#7F7F7F"
            def setcolor():
                self.backgroundcolor = colorchooser.askcolor()[1]
        
        def quitwin(event):
            self.quit()
            
        class fc:
            def colorblack():
                self.lbl.config(foreground="black")
            def colorgrey():
                self.lbl.config(foreground="#333333")
            def colorwhite():
                self.lbl.config(foreground="white")
            def colorblue():
                self.lbl.config(foreground="blue")
            def colorgreen():
                self.lbl.config(foreground="green")
            def coloryellow():
                self.lbl.config(foreground="yellow")
            def colorred():
                self.lbl.config(foreground="red")
            def colorpurple():
                self.lbl.config(foreground="purple")
            def colororange():
                self.lbl.config(foreground="orange")
            def colorbrown():
                self.lbl.config(foreground="brown")
            def colorpink():
                self.lbl.config(foreground="pink")
            def setcolor():
                self.lbl.config(foreground=colorchooser.askcolor()[1])

        def settrans():   #半透明
            if self.transbool==True:
                self.attributes("-alpha", 1)
                self.attributes("-alpha", 1)
            else:
                self.attributes("-alpha", 0.5)
                self.attributes("-alpha", 0.5)
            self.transbool=not self.transbool
            
        """主菜单"""
        
        menubar = tk.Menu(self,activeborderwidth=3)
        xMenu = tk.Menu(menubar, tearoff=True)
        yMenu = tk.Menu(menubar, tearoff=True)
        bMenu = tk.Menu(menubar, tearoff=True)

        xMenu.add_command(label="Ink Free",command=setfontarial)
        xMenu.add_command(label="Monotype Corsiva",command=setfontmc)
        xMenu.add_command(label="Ds-Digital",command=setfontdigital)
        xMenu.add_command(label="Impact",command=setfontimpact)
        xMenu.add_command(label="Segoe UI Black",command=setfontsegoe)
        xMenu.add_command(label="Comic Sans MS",command=setfontcomic)
        xMenu.add_command(label="Arial",command=setfontarialn)
        xMenu.add_command(label="Times New Roman",command=setfonttimes)
        
        yMenu.add_command(label="Red",command=bc.colorred)
        yMenu.add_command(label="Blue",command=bc.colorblue)
        yMenu.add_command(label="Green",command=bc.colorgreen)
        yMenu.add_command(label="Yellow",command=bc.coloryellow)
        yMenu.add_command(label="Black",command=bc.colorblack)
        yMenu.add_command(label="Grey",command=bc.colorgrey)
        yMenu.add_command(label="Purple",command=bc.colorpurple)
        yMenu.add_command(label="White",command=bc.colorwhite)
        yMenu.add_command(label="Orange",command=bc.colororange)
        yMenu.add_command(label="Brown",command=bc.colorbrown)
        yMenu.add_command(label="Pink",command=bc.colorpink)
        yMenu.add_command(label="Alpha",command=bc.coloralpha)
        yMenu.add_separator()
        yMenu.add_command(label="自定义颜色",command=bc.setcolor)

        bMenu.add_command(label="Red",command=fc.colorred)
        bMenu.add_command(label="Blue",command=fc.colorblue)
        bMenu.add_command(label="Green",command=fc.colorgreen)
        bMenu.add_command(label="Yellow",command=fc.coloryellow)
        bMenu.add_command(label="Black",command=fc.colorblack)
        bMenu.add_command(label="Grey",command=fc.colorgrey)
        bMenu.add_command(label="Purple",command=fc.colorpurple)        
        bMenu.add_command(label="White",command=fc.colorwhite)
        bMenu.add_command(label="Orange",command=fc.colororange)
        bMenu.add_command(label="Brown",command=fc.colorbrown)
        bMenu.add_command(label="Pink",command=fc.colorpink)
        bMenu.add_separator()
        bMenu.add_command(label="自定义颜色",command=fc.setcolor)
        
        menubar.add_cascade(label="选择字体", menu=xMenu,font=("ds-digital",10))     
        menubar.add_cascade(label="选择背景颜色", menu=yMenu,font=("ds-digital",10))
        menubar.add_cascade(label="选择文字颜色", menu=bMenu,font=("ds-digital",10))
        menubar.add_checkbutton(label="透明度",command=settrans,font=("ds-digital",10))
        menubar.add_command(label="退出时钟",command=self.quit,font=("ds-digital",10),accelerator="Ctrl+Q")
        menubar.add_checkbutton(label="全屏",command=activatefull,font=("ds-digital",10))
    
        def xShowMenu(event):
            menubar.post(event.x_root, event.y_root)   
         
        self.bind("<Button-3>", xShowMenu)           
        self.bind("<B1-Motion>",self.move)
        self.bind("<Right>",addl)
        self.bind("<Left>",del1)
        self.bind("<Control-Q>",quitwin)
        self.bind("<Control-q>",quitwin)
        
        self.lbl = tk.Label(self,
                            text=self.time_text+"",
                            font=(self.font, 20),
                            anchor="sw",
                            background="#333333",
                            foreground=lbl_fg) 
        self.lbl.place(x=30, y=0)
        self.update_time()
        
    def move(self, event):
        
        if self.fullbool:
            self.geometry(f"{self.width}x{self.height}+{(event.x + 7 - self.x) + self.winfo_x() - 7}+{(event.y - self.y) + self.winfo_y()}")
        pass
    
    
    def update_time(self):
        #a=self.lbl.winfo_reqheight()
        #b=self.lbl.winfo_reqwidth()
        
        self.geometry(f"{self.width}x{self.height}")        
        yy=int(str(datetime.datetime.now())[:4])
        mm=int(str(datetime.datetime.now())[5:7])
        dd=int(str(datetime.datetime.now())[8:10])
        k=datetime.date(yy,mm,dd).weekday()
        self.lbl.focus_set()
        
        if self.fullbool==False:
            self.lbl.config(anchor="sw",text=("\n"+str(datetime.datetime.now())[:10]+"\n"+str(datetime.datetime.now())[10:self.clocklen]+" "+self.days[k]),font=(self.font,self.fontsize),background=self.backgroundcolor)
        else:
            self.lbl.config(anchor="sw",text=(str(datetime.datetime.now())[:self.clocklen]+" "+self.days[k]),font=(self.font,self.fontsize),background=self.backgroundcolor)
        
        self.after(1, self.update_time)
        self.configure(bg=self.backgroundcolor)
        
        
if __name__=="__main__":
    Clock().mainloop()
 